<?php
class CourseController extends Controller
{
	public $layout="//layouts/nolayout";
	public function actionShowSchedulePage()
	{
		$this->render('showSchedulePage');
	}
	
	public function actionAddCoursePlanPage()
	{
		$this->render('addCoursePlanPage');
	}
	
	public function actionShowCourseInfoPage()
	{
		$this->render('showCourseInfoPage');
	}
	
	public function actionShowAllCourseInfoPage()
	{
		$this->render('showAllCourseInfoPage');
	}
	//添加新课程
	public function actionAddNewCourse()
	{
		if(isset($_POST['name'])&&isset($_POST['code'])&&isset($_POST['comment'])&&isset($_POST['type']))
		{
			$newCourse=new Course;
			$newCourse->name=$_POST['name'];
			$newCourse->code=$_POST['code'];
			$newCourse->comment=$_POST['comment'];
			$newCourse->type=$_POST['type'];
			if ($newCourse->save()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>'成功添加！'
						);   
				echo CJSON::encode($json);
			}else{
					$json=array(  
							'success'=>false,
							'res'=>'添加失败！'
						);   
					echo CJSON::encode($json);
			}
		}else{
					$json=array(  
							'success'=>false,
							'res'=>'数据发送失败！'
						);   
					echo CJSON::encode($json);
		}
	}
	
	//添加已有课程到用户
	public function actionAddCourseToUser()
	{
		if(!isset($_POST['name'])&&isset($_POST['course_id']))
		{
			$res=User_Course::model()->findByAttributes(array('course_id'=>$_POST['course_id'],'user_id'=>Yii::app()->user->id));
			if(!empty($res))
			{
				$json=array(
							'success'=>false,
							'res'=>'已经存在，无需再次添加'
						);   
				echo CJSON::encode($json);
				return;
			}
		
			$newUserCourse=new User_Course;
			$newUserCourse->user_id=Yii::app()->user->id;
			$newUserCourse->course_id=$_POST['course_id'];
			if ($newUserCourse->save()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>'成功添加！'
						);   
				echo CJSON::encode($json);
			}else
			{
				$newCourse->delete();
				$json=array(  
						'success'=>false,
						'res'=>'添加失败！'
					);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'数据发送失败！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	//编辑课程
	public function actionEditCourse()
	{
		if(isset($_POST['course_id'])&&isset($_POST['name'])&&isset($_POST['code'])&&isset($_POST['comment'])&&isset($_POST['type']))
		{
			$course=Course::model()->findByPk($_POST['course_id']);
			$course->name=$_POST['name'];
			$course->code=$_POST['code'];
			$course->comment=$_POST['comment'];
			$course->type=$_POST['type'];
			if ($course->save()>0)
			{
				$json=array(
							'success'=>true,
							'res'=>'成功修改！'
						);   
				echo CJSON::encode($json);
			}else
			{
				$json=array(  
						'success'=>false,
						'res'=>'修改失败！'
					);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'数据发送失败！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionDeleteCourse()
	{
		if(isset($_POST['course_id']))
		{
			$userCourse=Course::model()->findByPk($_POST['course_id']);
			if ($userCourse->delete()>0)
			{
					$json=array(  
								'success'=>true,
								'res'=>'成功删除！'
							);   
					echo CJSON::encode($json);
			}else{
				$json=array(  
						'success'=>false,
						'res'=>'删除失败！'
					);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'数据发送失败！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionDeleteUserCourse()
	{
		if(isset($_POST['course_id']))
		{
			$userCourse=User_Course::model()->findByAttributes(array('user_id'=>Yii::app()->user->id,'course_id'=>$_POST['course_id']));
			if ($userCourse->delete()>0)
			{
					$json=array(  
								'success'=>true,
								'res'=>'成功删除！'
							);   
					echo CJSON::encode($json);
			}else{
				$json=array(  
						'success'=>false,
						'res'=>'删除失败！'
					);   
				echo CJSON::encode($json);
			}
		}else
		{
			$json=array(  
						'success'=>false,
						'res'=>'数据发送失败！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	public function actionGetUserCourse()
	{
		$res=User::model()->findByPk(Yii::app()->user->id);
		$json=$res->Courses;
		echo CJSON::encode($json);
	}
	
	public function actionGetAllCourse()
	{
		$json=Course::model()->findAll();
		echo CJSON::encode($json);
	}
	
	public function actionGetAllVirtualCourse()
	{
		$res=Course::model()->findAllByAttributes(array('type'=>1));
		$json=$res;
		echo CJSON::encode($json);
	}
	//获得课程表格
	public function actionGetCourseGrid($term_id,$week_id)
	{
		$dayName=array('一','二','三','四','五','六','日');
		$modelRoomList=Room::model()->findAll(array('order'=>'name ASC'));
		//填充fields
		$fields=array();
		$elem=array();
		$elem['name']='dayOfWeek';
		$elem['type']='string';
		array_push($fields,$elem);
		
		$elem['name']='period';
		$elem['type']='string';
		array_push($fields,$elem);
		
		for($i=0;$i<count($modelRoomList);$i++)
		{
			$elem['name']=$modelRoomList[$i]->room_id;
			$elem['type']='string';
			array_push($fields,$elem);
		}
		//生成即将成为json的数组
		$json = array();
		$json['fields'] = $fields;
		//填充data
		$datas=array();
		$periodList=Period::model()->findAll();
		$nowSchedule=new Schedule;
		$elem=array();
		
		for ($dayi=0;$dayi<7;$dayi++)
		{
			//查询假期
			$start_date=Week::model()->findByPk($week_id)->start_date;
			$time=strtotime('+'.$dayi.' day '.$start_date);
			$date=date('Y-m-d',$time);
			$hasHoliday=Holiday::model()->find("`start_date` <= '".$date."' AND `end_date` >= '".$date."'");
			//如果有假期，则在这一天显示
			if (!empty($hasHoliday))
			{
				$elem['dayOfWeek']='周'.($dayName[$dayi]).'<p style="color:red">'.$hasHoliday->name.'</p>';
				for ($periodi=0;$periodi<count($periodList);$periodi++)
				{
					$elem['period']=$periodList[$periodi]->period_info;
					for($roomi=0;$roomi<count($modelRoomList);$roomi++)
						$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%,height:100%" data-qtitle="不可排课" data-qwidth="200" data-qtip="'.$hasHoliday->name.'"><p style="color:red">-</p></div>';
					array_push($datas,$elem);
					$elem=array();
					$elem['dayOfWeek']='';
				}
				continue;
			}
			else
			{
				$elem['dayOfWeek']='周'.($dayName[$dayi]);
			}
			
			for ($periodi=0;$periodi<count($periodList);$periodi++)
			{
				$elem['period']=$periodList[$periodi]->period_info;
				for($roomi=0;$roomi<count($modelRoomList);$roomi++)
				{
					$nowSchedule=Schedule::model()->findAllByAttributes(array(
											'term_id'=>$term_id,	
											'week_id'=>$week_id,
											'day_of_week'=>$dayi,
											'period_id'=>$periodList[$periodi]->period_id,
											'room_id'=>$modelRoomList[$roomi]->room_id,
											));
					if (!empty($nowSchedule))
					{
						//使用关系查询
						$text='';
						for ($i=0;$i<count($nowSchedule);$i++)
							$text=$text.$nowSchedule[$i]->user->name.'<br>'.$nowSchedule[$i]->course->name.'<br>';

						$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%,height:100%" data-qtitle="已排课" data-qwidth="200" data-qtip="['.$date.']<br>周'.($dayName[$dayi]).$periodList[$periodi]->period_info.'<br>'.$modelRoomList[$roomi]->name.'">'.$text.'</div>';
					}else{
						$elem[$modelRoomList[$roomi]->room_id]='<div style="width:100%,height:100%" data-qtitle="可排课" data-qwidth="100" data-qtip="['.$date.']<br>周'.($dayName[$dayi]).$periodList[$periodi]->period_info.'<br>'.$modelRoomList[$roomi]->name.'"><br><br></div>';
					}
				}
				array_push($datas,$elem);
				$elem=array();
				$elem['dayOfWeek']='';
			}
		}
		
		$json['data'] = $datas;
		//填充columns
		$columns=array();			
		$elem=array();
		$weekName=Week::model()->findByPk($week_id);
		$elem['header']=$weekName['week_describe'];
		$elem['dataIndex']='dayOfWeek';
		$elem['sortable']=false;
		$elem['width']='10%';
		array_push($columns,$elem);
		
		$elem['header']='节次';
		$elem['dataIndex']='period';
		$elem['sortable']=false;
		$elem['width']='10%';
		array_push($columns,$elem);
		
		for($i=0;$i<count($modelRoomList);$i++)
		{
			$elem['header']=$modelRoomList[$i]->name.'('.$modelRoomList[$i]->num_of_computer.')';
			$elem['dataIndex']=$modelRoomList[$i]->room_id;
			$elem['sortable']=false;
			$elem['width']=intval(80/count($modelRoomList)).'%';
			array_push($columns,$elem);
		}
		$json['columns'] = $columns;
		
		echo CJSON::encode($json);
	}
	//添加新的排课
	public function actionAddNewSchedule($tid,$wid,$rid,$period,$cid)
	{
		//查询假期
		$start_date=Week::model()->findByPk($wid)->start_date;
		$periodList=Period::model()->findAll();
		$day=intval($period/(count($periodList)));
		$time=strtotime('+'.$day.' day '.$start_date);
		$date=date('Y-m-d',$time);
		$hasHoliday=Holiday::model()->find("`start_date` <= '".$date."' AND `end_date` >= '".$date."'");
		if (!empty($hasHoliday))
		{
			$json=array(  
						'success'=>false,
						'res'=>'添加失败！'
					);   
			echo CJSON::encode($json);
			return;
		}
		
		$newSchedule=new Schedule;
		$newSchedule->course_id=$cid;
		$newSchedule->user_id=Yii::app()->user->id;
		$newSchedule->term_id=$tid;
		$newSchedule->room_id=$rid;
		$newSchedule->week_id=$wid;
		$periodList=Period::model()->findAll();
		$day=intval($period/(count($periodList)));
		$newSchedule->day_of_week=intval($day);
		$pindex=$period%(count($periodList));
		$newSchedule->period_id=$periodList[$pindex]->period_id;
		$res=Schedule::model()->findByAttributes(array(
											'term_id'=>$tid,	
											'week_id'=>$wid,
											'day_of_week'=>intval($day),
											'period_id'=>$periodList[$pindex]->period_id,
											'room_id'=>$rid,
											'user_id'=>Yii::app()->user->id
											));
		if (!empty($res))
		{
			$res->course_id=$cid;
			$newSchedule=$res;
		}
		
		if ($newSchedule->save()>0)
		{
			$json=array(  
						'success'=>true,
						'res'=>'添加成功',
					);   
			echo CJSON::encode($json);
		}else{
			$json=array(  
						'success'=>false,
						'res'=>'添加失败！'
					);   
			echo CJSON::encode($json);
		}
	}
	//检查排课权限
	public function actionHasScheduleAccess($tid,$wid,$rid,$period)
	{
		$periodList=Period::model()->findAll();
		$day=intval($period/(count($periodList)));
		$pindex=$period%(count($periodList));
		//查询假期
		$start_date=Week::model()->findByPk($wid)->start_date;
		$periodList=Period::model()->findAll();
		$time=strtotime('+'.$day.' day '.$start_date);
		$date=date('Y-m-d',$time);
		$hasHoliday=Holiday::model()->find("`start_date` <= '".$date."' AND `end_date` >= '".$date."'");
		if (!empty($hasHoliday))
		{
			$json=array(  
						'success'=>false,
						'res'=>'添加失败！'
					);   
			echo CJSON::encode($json);
			return;
		}
		
		$json=array(  
					'success'=>true,
					'res'=>'',
				);   
		echo CJSON::encode($json);
		
	}
	//取消排课
	public function actionDeleteSchedule($tid,$wid,$rid,$period) 
	{
		$periodList=Period::model()->findAll();
		$day=$period/(count($periodList));
		$pindex=$period%(count($periodList));
		$res=Schedule::model()->findByAttributes(array(
											'term_id'=>$tid,	
											'week_id'=>$wid,
											'day_of_week'=>intval($day),
											'period_id'=>$periodList[$pindex]->period_id,
											'room_id'=>$rid,
											'user_id'=>Yii::app()->user->id
											));
		if (!empty($res))
		{
			if ($res->delete()>0)
			{
				$json=array(  
							'success'=>true,
							'res'=>'删除成功',
						);   
				echo CJSON::encode($json);
			}else{
				$json=array(  
						'success'=>false,
						'res'=>'无法删除！'
					);   
				echo CJSON::encode($json);
			}
		}else{
			$json=array(  
						'success'=>false,
						'res'=>'你没有这个权限！'
					);   
			echo CJSON::encode($json);
		}
	}
	
	//获得批量排课信息
	public function actionGetCoursePlanChecked($tid,$day,$pid,$rid,$cid)
	{
		$CoursePlanList=Schedule::model()->findAllByAttributes(array(
											'term_id'=>$tid,
											'day_of_week'=>$day,
											'period_id'=>$pid,
											'room_id'=>$rid,
											'course_id'=>$cid,
											));
		$json=array();
		for ($i=0;$i<count($CoursePlanList);$i++)
		{
			$elem=array();
			$elem['wid']=$CoursePlanList[$i]->week_id;
			array_push($json,$elem);
		}
		echo CJSON::encode($json);
	}
	//异步排课
	public function actionSetSchedule($oid,$tid,$wid,$day,$pid,$rid,$cid,$status)
	{
		if ($status=='true')
		{
			//查询假期
			$start_date=Week::model()->findByPk($wid)->start_date;
			$time=strtotime('+'.$day.' day '.$start_date);
			$date=date('Y-m-d',$time);
			$hasHoliday=Holiday::model()->find("`start_date` <= '".$date."' AND `end_date` >= '".$date."'");
			if (!empty($hasHoliday))
			{
				$json=array(  
							'success'=>false,
							'oid'=>$oid,
							'res'=>'<a style="color:red">冲突！假期名称：'.$hasHoliday->name.'</a>'
						);   
				echo CJSON::encode($json);
				return;
			}
			/*
			$Schedule=Schedule::model()->findByAttributes(array(
													'term_id'=>$tid,
													'week_id'=>$wid,
													'day_of_week'=>$day,
													'period_id'=>$pid,
													'room_id'=>$rid
													));
			//在此已有课程了
			if (!empty($Schedule))
			{
				$json=array(  
						'success'=>false,
						'oid'=>$oid,
						'res'=>'<a style="color:red">冲突！课程名称：'.$Schedule->course->name.'  课程教师：'.$Schedule->user->name.' 联系电话：'.$Schedule->user->tel.'</a>'
					);   
				echo CJSON::encode($json);
				return;
			}
			*/
			$Schedule=Schedule::model()->findByAttributes(array(
													'term_id'=>$tid,
													'week_id'=>$wid,
													'day_of_week'=>$day,
													'period_id'=>$pid,
													'room_id'=>$rid,
													'course_id'=>$cid,
													'user_id'=>Yii::app()->user->id
													));
			if (!empty($Schedule))
			{
				$newSchedule=$Schedule;
			}
			else{
				$newSchedule=new Schedule;
			}
			$newSchedule->term_id=$tid;
			$newSchedule->week_id=$wid;
			$newSchedule->day_of_week=$day;
			$newSchedule->period_id=$pid;
			$newSchedule->room_id=$rid;
			$newSchedule->course_id=$cid;
			$newSchedule->user_id=Yii::app()->user->id;
			if ($newSchedule->save()>0)
			{
				$json=array(  
						'success'=>true,
						'oid'=>$oid,
						'res'=>'<a style="color:green">排课成功！</a>'
					);   
				echo CJSON::encode($json);
			}else
			{
				$json=array(  
						'success'=>false,
						'oid'=>$oid,
						'res'=>'<a style="color:red">排课失败</a>'
					);
				echo CJSON::encode($json);
			}
		}else{
			$Schedule=Schedule::model()->findByAttributes(array(
													'term_id'=>$tid,
													'week_id'=>$wid,
													'day_of_week'=>$day,
													'period_id'=>$pid,
													'room_id'=>$rid,
													'course_id'=>$cid,
													'user_id'=>Yii::app()->user->id
													));
			if (!empty($Schedule))
			{
				if ($Schedule->delete()>0)
				{
					$json=array(  
						'success'=>true,
						'oid'=>$oid,
						'res'=>'<a style="color:green">取消排课成功</a>'
					);   
					echo CJSON::encode($json);
				}else{
					$json=array(  
						'success'=>false,
						'oid'=>$oid,
						'res'=>'<a style="color:red">取消排课失败<a>'
					);   
					echo CJSON::encode($json);
				}
			}else{
				$json=array(  
						'success'=>false,
						'oid'=>$oid,
						'res'=>'未改动'
					);   
				echo CJSON::encode($json);
			}			
		}
	}
}
?>